package com.minelx.datamicroscope.core.condition;

import com.alibaba.fastjson.JSONObject;
import com.minelx.datamicroscope.core.values.CommonField;

import java.util.List;

public interface Operand {

	String sqlOf(CommonField key);

	JSONObject toJSON();

	static Operand lt(String value) {
		return new OperatorOperand("lt", "<", value);
	}

	static Operand gt(String value) {
		return new OperatorOperand("gt", ">", value);
	}

	static Operand le(String value) {
		return new OperatorOperand("le", "<=", value);
	}

	static Operand ge(String value) {
		return new OperatorOperand("ge", ">=", value);
	}

	static Operand eq(String value) {
		return new OperatorOperand("eq", "=", value);
	}

	static Operand ne(String value) {
		return new OperatorOperand("ne", "!=", value);
	}

	static Operand in(List<String> values) {
		return new InOperand(values);
	}

	static Operand from(JSONObject data) {
		switch (data.getString("operator")) {
			case "lt":
				return lt(data.getString("value"));
			case "gt":
				return gt(data.getString("value"));
			case "le":
				return le(data.getString("value"));
			case "ge":
				return ge(data.getString("value"));
			case "eq":
				return eq(data.getString("value"));
			case "ne":
				return ne(data.getString("value"));
			case "in":
				return in(data.getJSONArray("value").toJavaList(String.class));
			default:
				throw new IllegalArgumentException("unexpected operator: " + data.getString("operator"));
		}
	}
}
